package com.ichangtou.dz.leetcode.question2;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.util.LinkedList;

/**
 * @author: jiyasai
 * @date: 2019-06-10 22:24
 * @version: 1.0.0
 * @description: 给出两个 非空 的链表用来表示两个非负的整数。其中，它们各自的位数是按照 逆序 的方式存储的，并且它们的每个节点只能存储 一位 数字。
 * 如果，我们将这两个数相加起来，则会返回一个新的链表来表示它们的和。
 * 您可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 * 示例：
 * 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出：7 -> 0 -> 8
 * 原因：342 + 465 = 807
 * @modified: <修改说明>
 **/
public class AddTwoNumbers {


    /**
     * 定义数据链表节点
     */
    @Data(staticConstructor = "of")
    @Accessors(chain = true)
    public static class NumNode {
        /**
         * 当前值
         */
        int val;

        /**
         * 下一个节点
         */
        NumNode next;


    }


    private static NumNode numberNode_1 = NumNode.of()
            .setVal(2)
            .setNext(NumNode.of()
                    .setVal(4)
                    .setNext(NumNode.of()
                            .setVal(3)
                            .setNext(null)));

    private static NumNode numberNode_2 = NumNode.of()
            .setVal(5)
            .setNext(NumNode.of()
                    .setVal(6)
                    .setNext(NumNode.of()
                            .setVal(4)
                            .setNext(null)));

    /**
     * 将数字字符串转为链表
     * 链表第一位为最低位，链表最后一位为最高位
     * @param numStr
     * @return
     */
    private static NumNode strToNumNode(String numStr) {
        byte[] numStrBytes = numStr.getBytes();
        NumNode numNode = NumNode.of();
        NumNode nextNode = null;
        for (int i = (numStrBytes.length-1); i >= 0; i++) {
            nextNode = NumNode.of()
                    .setVal(i);
            numNode.setNext(nextNode);

        }
        return numNode;
    }



    public static void main(String[] args) {
        NumNode twoNumbers = addTwoNumbers(numberNode_1, numberNode_2);
        System.out.println(twoNumbers.getVal());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(twoNumbers.getVal());
        NumNode nextNode = twoNumbers.getNext();
        while (nextNode != null) {
            stringBuilder.append(nextNode.getVal());
            nextNode = nextNode.getNext();
        }
        StringBuilder builder = stringBuilder.reverse();
        System.out.println(builder);
    }


    public static NumNode addTwoNumbers(NumNode l1, NumNode l2) {
        NumNode dummyHead = NumNode.of().setVal(0);
        NumNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = NumNode.of().setVal(sum % 10);
            curr = curr.next;
            if (p != null) {
                p = p.next;
            }
            if (q != null) {
                q = q.next;
            }
        }
        if (carry > 0) {
            curr.next = NumNode.of().setVal(carry);
        }
        return dummyHead.next;
    }



}
